/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package kernel.stl;

import java.util.ArrayList;
import kernel.stl.exception.InvalidPointInsertion;

/**
 *
 * @author George
 */
public class PointXYList {
    
    private ArrayList list = new ArrayList();
    
    public void addPoint(PointXY point) throws InvalidPointInsertion {
        if (this.hasPoint(point))
            throw new InvalidPointInsertion();            
        list.add(point);
    }
    
    public boolean hasPoint(PointXY point) {
        return this.indexOf(point) != -1;
    }
    
    public int count() {
        return list.size();
    }
    
    public PointXY getPoint(int index) {
        return (PointXY) list.get(index);        
    }
    
    public int indexOf(PointXY point) {
        for (int i = 0; i < this.count(); i++)
            if (this.getPoint(i).equals(point))
                return i;
        return -1;
    }
    
    public int indexOfNearestPoint(PointXY point) {
        double smaller = -1;
        int index = -1;
        
        for (int i = 0; i < this.count(); i++) {
            if (!this.getPoint(i).equals(i)) {
                double distance = this.getPoint(i).distanceTo(point);
                if ((smaller == -1 || smaller > distance) && (distance > 0)) {
                    smaller = distance;
                    index = i;
                }
            }
        }
        return index;
    }
    
    

}
